<?php
/*************************************************************************************
   Copyright notice

   This script uses some code parts of the glossary module for phpwcms (c) Oliver Georgi
   (c) 2002-2009 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.

   This script uses some code parts from bmcatalog module
   (c) 2010 Benjamin Matzmorr, www.etype.de

   (c) 2012 webrealisierung gmbh, www.webrealisierung.ch
   Catalogue Module V 1.0.0  -  Date: 2012-06-01

   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license
   from the author is found in LICENSE.txt distributed with these scripts.

   This script is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.

   This copyright notice MUST APPEAR in all copies of the script!
*************************************************************************************/

// Module/Plug-in catalogue cp class
class ModuleCatalogue {

  var $plugin_ctg = array();
  var $plugin_ctg_active = array();  //[artid][array active catalogs in this article]
  var $plugin_ctg_catalogs = array(); //all catalogs from db
  var $plugin_articlelist = array();
  var $plugin_cplist = array();
  var $plugin_catalogs = array(); //all catalogs, array with key=id

  //get the catalogs
	function ctg_get_ctlgs() {
    $sql = "SELECT * FROM ".DB_PREPEND."phpwcms_mod_catalogue_catalog";
    $plugin_catalogsresult =  _dbQuery($sql);
    $this->plugin_ctg_catalogs = $plugin_catalogsresult;
      foreach($plugin_catalogsresult as $row) {
        $this->plugin_catalogs[$row['ctgc_id']] = $row;
      }
  }

  //get the catalogs(ID) of catalogs selected in any contentparts in any articles
  //and set the $plugin_ctg_active
  //it's not super perfect cause more or less the same thing is done in the standard search yet
	function ctg_get_articles() {

 		$sql  = "SELECT article_id, article_cid, article_uid, article_title, article_username, ";
		$sql .= "article_aktiv, article_public, UNIX_TIMESTAMP(article_tstamp) AS article_date, ac.acat_id, ac.acat_name, ac.acat_aktiv, ac.acat_public, ac.acat_hidden, ac.acat_regonly  ";
		$sql .= "FROM ".DB_PREPEND."phpwcms_article ar ";
		$sql .= "LEFT JOIN ".DB_PREPEND."phpwcms_articlecat ac ON ";
		$sql .= "(ar.article_cid = ac.acat_id OR ar.article_cid = 0)";
		$sql .= " WHERE ";
		//limit to special structure IDs if not all
//		if(count($content["search"]["start_at"])) {
//			$sql .= 'ar.article_cid IN ('.implode(',', $content["search"]["start_at"]).')';
//		} else {
//			$sql .= "IF(ar.article_cid = 0, " . (empty($GLOBALS['indexpage']['acat_nosearch']) ? 1 : 0) .", 1)";
//		}
//		$sql .= "ac.acat_aktiv=1 AND ac.acat_public=1 AND ";
//		if(!FEUSER_LOGIN_STATUS) {
//			$sql .= "ac.acat_regonly=0 AND ";
//		}
//		$sql .= "ar.article_public=1 AND ar.article_aktiv=1 AND ar.article_deleted=0 AND ar.article_nosearch!=1 AND ";
		$sql .= "ar.article_deleted=0 AND ";
		// enhanced IF statement by kh 2008/12/03
		$sql .= "IF((ar.article_begin < NOW() AND ar.article_end > NOW()) OR (ar.article_archive_status=1 AND ac.acat_archive=1), 1, 0) ";
		$sql .= "GROUP BY ar.article_id";
  	$plugin_sresult = _dbQuery($sql);
    $this->plugin_articlelist = $plugin_sresult;
      foreach($plugin_sresult as $row) {
				// read article content for update
//			$cm_art_sql  = "SELECT acontent_id, acontent_aid, acontent_title, acontent_type, acontent_form, acontent_tstamp, acontent_visible FROM ";
				$cm_art_sql  = "SELECT * FROM ";
				$cm_art_sql .= DB_PREPEND."phpwcms_articlecontent WHERE acontent_aid=".$row["article_id"]." ";
				$cm_art_sql .= "AND acontent_module='br_catalogue' ";
//				$cm_art_sql .= "AND acontent_visible=1 AND acontent_trash=0 AND ";
				$cm_art_sql .= "AND acontent_trash=0 AND ";
//				if( !FEUSER_LOGIN_STATUS ) {
//					$cm_art_sql .= 'acontent_granted=0 AND ';
//				}
				$cm_art_sql .= "acontent_type = 30";
  	    $plugin_cplist = _dbQuery($cm_art_sql);

          foreach ($plugin_cplist as $artrow) {
            $artrow["artdata"] = $row;
            $artrow['acontent_form'] = @unserialize($artrow['acontent_form']);
            $this->plugin_cplist[$artrow['acontent_id']] = $artrow;
//if ($artrow['acontent_form']['catalogs'] == '') $artrow['acontent_form']['catalogs'] = array();  //compatibilty to V1.0
              $plugin_ctg_art = array();
                 foreach($this->plugin_ctg_catalogs as $value_cal) {
                  if (in_array($value_cal['ctgc_id'], $artrow['acontent_form']['ctg_catalogs'])){
                    array_push($plugin_ctg_art, $value_cal['ctgc_id']);
                  }
                 }
              $this->plugin_ctg_active[$artrow['acontent_aid']] =  $plugin_ctg_art;
          }
      }
  }

}
?>